package com.perceptivesoftware.licensing.client.dialogs.systemparm;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.perceptivesoftware.licensing.client.LicensingAsyncCallback;
import com.perceptivesoftware.licensing.client.LicensingRPCFactory;
import com.perceptivesoftware.licensing.client.common.util.SystemParmType;
import com.perceptivesoftware.licensing.client.dto.SystemParmDTO;

/**
 * <p/>
 * Copyright 2012 Perceptive Software
 * <p/>
 * Project: Perceptive Licensing
 * <p/>
 * Confidential and Proprietary
 * 
 * @author Nolan Brassard
 * 
 */
public class SystemEmailHostDialog extends Dialog {

	private TextField<String> email;
	private TextField<String> smtphost;
	private TextField<String> smtpport;
	private TextField<String> smtpUser;
	private TextField<String> smtpPass;
	private final MessageBox mb;

	public SystemEmailHostDialog() {
		super();
		setHeading("Configure System Email");
		setModal(true);
		setButtons(Dialog.OKCANCEL);
		setButtonAlign(HorizontalAlignment.CENTER);
		buildUI();
		buildButtons();

		mb = MessageBox.wait("Loading", "Loading", "Loading...");
		LicensingRPCFactory.getSystemService(true).getSystemParmById(SystemParmType.EMAIL_HOST, new LicensingAsyncCallback<SystemParmDTO>() {

			@Override
			public void onSuccess(final SystemParmDTO result) {
				setData(result);
			}
		});
	}

	public void setData(SystemParmDTO dto) {
		mb.close();
		if (dto != null) {
			String fields = dto.getParmVal();
			int next = fields.indexOf("|");
			String nextField;

			for (int x = 1; x <= 5; x++) {
				if (next == 0) {// empty field
					nextField = "";
				}
				else if (next == -1) {
					nextField = fields;
					if (x == 5) {
						smtpPass.setValue(nextField);
					}
				}
				else {
					nextField = fields.substring(0, next);

					switch (x) {
					case 1:
						email.setValue(nextField);
						break;
					case 2:
						smtphost.setValue(nextField);
						break;
					case 3:
						smtpport.setValue(nextField);
						break;
					case 4:
						smtpUser.setValue(nextField);
						break;
					case 5:
						smtpPass.setValue(nextField);
						break;
					}
				}

				if (next == -1) {// no more fields
					break;
				}
				else if (next < fields.length()) {
					fields = fields.substring(next + 1);
					next = fields.indexOf("|");
				}
			}
		}
	}

	private void buildUI() {
		setWidth(400);
		setResizable(false);
		setLayout(new FitLayout());

		final FormPanel form = new FormPanel();
		form.setLayout(new FormLayout());
		form.setFrame(true);
		form.setHeaderVisible(false);
		form.setAutoHeight(true);
		form.setBorders(false);

		email = new TextField<String>();
		email.setFieldLabel("Email");
		form.add(email, new FormData("-10"));

		final FieldSet serverInfo = new FieldSet();
		serverInfo.setHeading("Server Information (SMTP)");
		serverInfo.setLayout(new FormLayout());

		smtphost = new TextField<String>();
		smtphost.setFieldLabel("Host");
		serverInfo.add(smtphost, new FormData("-10"));

		smtpport = new TextField<String>();
		smtpport.setFieldLabel("Port");
		serverInfo.add(smtpport, new FormData("-10"));

		smtpUser = new TextField<String>();
		smtpUser.setFieldLabel("Username");
		serverInfo.add(smtpUser, new FormData("-10"));

		smtpPass = new TextField<String>();
		smtpPass.setFieldLabel("Password");
		smtpPass.setPassword(true);
		serverInfo.add(smtpPass, new FormData("-10"));

		form.add(serverInfo, new FormData("0"));

		add(form);
	}

	private void buildButtons() {
		final Button okButton = getButtonById("ok");
		okButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(final ButtonEvent ce) {
				if (validateFields()) {
					LicensingRPCFactory.getSystemService(true).saveSystemParm(saveFieldsToDTO(), new LicensingAsyncCallback<SystemParmDTO>() {

						@Override
						public void onSuccess(final SystemParmDTO result) {
							hide();
						}
					});
				}
			}
		});

		final Button cancelButton = getButtonById("cancel");
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(final ButtonEvent ce) {
				hide();
			}
		});

	}

	protected boolean validateFields() {
		boolean retValue = true;

		if ((email.getValue() == null) || (email.getValue().length() == 0)) {
			email.setInputStyleAttribute("border-color", "red");
			retValue = false;
		}
		else {
			email.setInputStyleAttribute("border-color", "darkgray");
		}
		if ((smtphost.getValue() == null) || (smtphost.getValue().length() == 0)) {
			smtphost.setInputStyleAttribute("border-color", "red");
			retValue = false;
		}
		else {
			smtphost.setInputStyleAttribute("border-color", "darkgray");
		}
		if ((smtpport.getValue() == null) || (smtpport.getValue().length() == 0)) {
			smtpport.setInputStyleAttribute("border-color", "red");
			retValue = false;
		}
		else {
			smtpport.setInputStyleAttribute("border-color", "darkgray");
		}
		if ((smtpUser.getValue() == null) || (smtpUser.getValue().length() == 0)) {
			smtpUser.setInputStyleAttribute("border-color", "red");
			retValue = false;
		}
		else {
			smtpUser.setInputStyleAttribute("border-color", "darkgray");
		}
		if ((smtpPass.getValue() == null) || (smtpPass.getValue().length() == 0)) {
			smtpPass.setInputStyleAttribute("border-color", "red");
			retValue = false;
		}
		else {
			smtpPass.setInputStyleAttribute("border-color", "darkgray");
		}

		if (retValue == false) {
			MessageBox.alert(getHeading(), "Some of the required fields were incomplete or incorrect.", null);
		}

		return retValue;
	}

	protected SystemParmDTO saveFieldsToDTO() {
		final SystemParmDTO newDto = new SystemParmDTO();
		newDto.setId(SystemParmType.EMAIL_HOST);
		final String parmval = email.getValue() + "|" + smtphost.getValue() + "|" + smtpport.getValue() + "|" + smtpUser.getValue() + "|" + smtpPass.getValue();
		newDto.setParmVal(parmval);

		return newDto;
	}
}